package com.xudow.app.ui.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.activeshare.edu.ucenter.models.vo.AttendanceVO;
import com.xudow.app.R;

import java.util.List;

/**
 * 考勤记录ListView Adapter
 *
 * @author lukecheng
 */
public class ClazzLogAdapter extends RecyclerView.Adapter {

    private List<AttendanceVO> attendances;

    public ClazzLogAdapter(List<AttendanceVO> attendances) {
        this.attendances = attendances;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_class_log, null);

        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        view.setLayoutParams(lp);

        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final ViewHolder viewHolder = (ViewHolder) holder;

        AttendanceVO vo = attendances.get(position);
        viewHolder.info.setText(vo.getInfo());

        viewHolder.root.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (1 == viewHolder.info.getLineCount()) {
                    viewHolder.info.setSingleLine(false);
                } else {
                    viewHolder.info.setSingleLine(true);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return attendances.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        private LinearLayout root;
        public TextView info;

        public ViewHolder(View itemView) {
            super(itemView);

            root = (LinearLayout) itemView.findViewById(R.id.root);
            info = (TextView) itemView.findViewById(R.id.info);
        }
    }
}
